<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BASpaceCooker
 *
 * @author jianfeng
 */
class BSpaceCooker extends BCooker {

    const Space_Type_Personal = 'Space_type_personal';
    const Space_Type_Not_Personal = 'space_type_not_personal';
    const Space_Type_All = 'space_type_all';

    public function getSpace($spaceData) {
        if ($spaceData instanceof BSpace) {
            return $spaceData;
        }
        if (is_numeric($spaceData) && $spaceData > 0) {
            return new BSpace($spaceData);
        }
        if (is_string($spaceData) && $spaceData != NULL) {
            $mySpaceId = $spaceData;
            return BSpace::find(array('myId' => $mySpaceId));
        }
    }

    public function getSpaceInfoItemValue($name, $searchData = NULL) {
        if ($searchData != NULL) {
            $spaceInfo = $this->getSpaceInfo($searchData);
        } else {
            $spaceInfo = $this->getCurrentSpaceInfo();
        }
        if ($spaceInfo instanceof BTreeList) {
            if ($spaceInfo->getItem($name) != NULL) {
                return $spaceInfo->getItem($name)->getValue();
            }
        }
    }

    public function getCurrentSpaceInfo() {
        $spaceId = Blyn::app()->getActiveSpace()->getId();
        return $this->getSpaceInfo($spaceId);
    }

    public function getSpaceInfo($spaceInfoData) {

        if (is_numeric($spaceInfoData) && $spaceInfoData > 0) {
            $spaceId = $spaceInfoData;
            $itemData = array('spaceId' => $spaceId);
            $sTable = $this->getSpaceInfoTable();
            return $sTable->getList(array('itemData' => $itemData));
        }
    }

    public function getSpaceInfoTable($typeData = NULL) {
        $type = NULL;
        if ($typeData != NULL) {
            $type = $this->getSpaceType($typeData);
        }
        if ($type == NULL) {
            return new BlynSheet('spaceInfo', self::Space_Type_All);
        } else {
            return new BlynSheet('spaceInfo', $type);
        }
    }

    /**
     * 返回可以的数据库连接名称
     * @return type
     */
    public function getSpaceRepositoryList() {
        return array('bSpaceDb1' => 'bSpaceDb1', 'bSpaceDb2' => 'bSpaceDb2');
    }

    /**
     * 返回机构可以用的数据库连接,没有指定名称，就返回当前的机构的连接
     * @param type $name
     * @return type
     */
    public function getSpaceRepository($name = NULL) {
        if ($name == NULL) {
            $name = $this->getSpaceInfoItemValue('dbRepository');
        }
        if ($name != NULL) {
            $connString = 'mysql:host=localhost;dbname=' . $name;

            $emulatePrepare = true;
            $userName = 'test';
            $password = 'password';
            $charset = 'utf8';
            $tblPrefix = 'tbl_';
            $enableParamLogging = true;

            if ($connString != NULL && $userName != NULL && $password != NULL)
                $conn = new CDbConnection($connString, $userName, $password);

            if ($conn instanceof CDbConnection) {
                if ($emulatePrepare != NULL)
                    $conn->emulatePrepare = $emulatePrepare;
                if ($charset != NULL)
                    $conn->charset = $charset;
                if ($tblPrefix != NULL)
                    $conn->tablePrefix = $tblPrefix;
                //$conn->active = TRUE;
                return $conn;
            }
        }
        return NULL;
    }

    //put your code here

    public function getUserSpaceList($format = 'spaceList') {

        $userRoles = BRole::findUserRoles();

        $spaceList = array();
        foreach ($userRoles as $role) {
            $spaceId = $role->spaceId;
            if (is_numeric($spaceId) && $spaceId > 0)
                $spaceList[$spaceId] = new BSpace($spaceId);
        }

        return $spaceList;
    }

    public function getApplySpaceList() {

        $spaceList = BSpace::findAll();
    }

    protected function getSpaceTypeRoot() {

        $spaceCategoryRoot = $this->getSpaceCategoryRoot();
        $spaceTypeRoot = $spaceCategoryRoot->addChild('spaceType');
        return $spaceTypeRoot;
    }

    protected function getSpaceCategoryRoot() {

        $rootNode = BCategory::getRoot();
        $spaceCategoryRoot = $rootNode->addChild('spaceCategory_root');
        return $spaceCategoryRoot;
    }

    public function addSpaceType($typeName) {
        $sTypeRoot = $this->getSpaceTypeRoot();
        $type = $sTypeRoot->addChild($typeName);
        return $type;
    }

    public function getSpaceType($spaceData = NULL) {
        if (is_numeric($spaceData) && $spaceData > 0) {
            return new BCategory($spaceData);
        }

        if (is_string($spaceData) && $spaceData != NULL) {
            $spaceTypeRoot = $this->getSpaceTypeRoot();
            return $spaceTypeRoot->getChild($spaceData);
            /*
              if (strpos($spaceData, 'spaceType')) {
              return $spaceTypeRoot->getChild($spaceData);
              } else {
              $spaceTypeName = 'spaceType.' . $spaceData;
              return $spaceTypeRoot->getChild($spaceTypeName);
              }
             * 
             */
        }
    }

    /**
     * add space information into a table of spaceInfo
     * @param type $spaceData
     * @return BTreeList Description
     */
    public function addSpaceInfo($spaceData, $spaceType = NULL) {

        $spaceInfo = NULL;
        if ($spaceType != NULL) {
            $oSpaceType = $this->getSpaceType($spaceType);
            if ($oSpaceType == NULL && is_string($spaceType) && $spaceType != NULL) {
                $oSpaceType = $this->addSpaceType($spaceType);
            }
            $spaceInfoTable = new BlynSheet('spaceInfo', $oSpaceType);
        }

        if ($spaceType == NULL) {
            $spaceInfoTable = new BlynSheet('spaceInfo', self::Space_Type_All);
        }

        if (is_array($spaceData)) {
            $spaceId = NULL;
            if (isset($spaceData['spaceId']) && $spaceData['spaceId'] != NULL) {
                $spaceId = $spaceData['spaceId'];
            }
            if (isset($spaceData['id']) && $spaceData['id'] != NULL) {
                $spaceId = $spaceData['id'];
                if (is_numeric($spaceId) && $spaceId > 0) {
                    unset($spaceData['id']);
                    $spaceData['spaceId'] = $spaceId;
                }
            }
            if (is_numeric($spaceId) && $spaceId > 0) {
                $oSpaceInfo = $spaceInfoTable->getList(array('itemData' => array('spaceId' => $spaceId)));
                if ($oSpaceInfo != NULL) {
                    $oSpaceInfo->addItems($spaceData);
                    return $oSpaceInfo;
                }
            }
            $spaceInfo = $spaceInfoTable->addList($spaceData);
        }
        return $spaceInfo;
    }

    public function getSpaceInfoView($spaceType = NULL, $spaceData = NULL) {

        $spaceInfoTable = NULL;
        if ($spaceType != NULL) {
            $oSpaceType = $this->getSpaceType($spaceType);
            if ($oSpaceType instanceof BCategory) {
                $spaceInfoTable = new BlynSheet('spaceInfo', $oSpaceType);
            }
        }
        if ($spaceInfoTable == NULL) {
            $spaceInfoTable = new BlynSheet('spaceInfo', BTreeTable::All_Owner);
        }

        if (is_array($spaceData) && $spaceData != NULL) {
            $view = $spaceInfoTable->getView(array('itemData' => $spaceData));
        } else {
            $view = $spaceInfoTable->getView();
        }

        if ($spaceType == self::Space_Type_Not_Personal) {
            $personalType = $this->getSpaceType(self::Space_Type_Personal);
            if ($personalType instanceof BCategory) {
                $typeId = $personalType->getId();
                foreach ($view as $id => $list) {
                    if ($list->getOwnerId() == $typeId) {
                        unset($view[$id]);
                    }
                }
            }
        }
        return $view;
    }

    public function getUserJoinableSpaceList($user = NULL) {
        $userSpaceList = $this->getUserSpaceList();
        $idList = array_keys($userSpaceList);
        $spaceInfoList = $this->getSpaceInfoView(self::Space_Type_Not_Personal);

        foreach ($spaceInfoList as $id => $list) {
            $spaceData = $list->getItemsInArray();
            if (isset($spaceData['spaceId'])) {
                $spaceId = $spaceData['spaceId'];
                if (in_array($spaceId, $idList)) {
                    unset($spaceInfoList[$id]);
                }
            }
        }

        return $spaceInfoList;
    }

    public function onShowSpaceListForJoin() {

        $spaceList = $this->getUserJoinableSpaceList();

        $this->updateControl->addRenderView('spaceListForJoin', 'spaceListForJoin', array('spaceList' => $spaceList));
    }

    public function onShowJoinSpace() {

        $spaceId = $this->activeLink->getLinkVariable('spaceId');

        $roleCooker = new BARoleCooker();
        $spaceCustomerRole = $roleCooker->getRole(BRole::Role_Space_customer, $spaceId);
        $user = Blyn::app()->getCurrentUser();
        $spaceCustomerRole->addMember($user);
        
        $this->onShowSpaceListForJoin();
        
        /*
        $spaceRoleData = $roleCooker->getAllSpaceRoleData(NULL, NULL, $spaceId);
        $control = $this->getCookerControl();
        $space = new BSpace($spaceId);
        //$this->updateControl->addChildControl($control);
        $control->addRenderView('spaceRoleList', 'spaceRoleList', array('spaceRoleData' => $spaceRoleData, 'space' => $space));
         * 
         */
    }

    public function onJoinSpaceRole() {
        $roleId = $this->activeLink->getLinkVariable('roleId');
        $roleCooker = new BARoleCooker();
        $user = Blyn::app()->getCurrentUser();
        $role = new BRole($roleId);
        $role->addMember($user);

        $this->onShowSpaceListForJoin();
    }

}
